# from threading import Thread, Event, Timer
# import time
#
# def TimerReset(*args, **kwargs):
#     """ Global function for Timer """
#     return _TimerReset(*args, **kwargs)
#
#
# class _TimerReset(Thread):
#     """Call a function after a specified number of seconds:
#
#     t = TimerReset(30.0, f, args=[], kwargs={})
#     t.start()
#     t.cancel() # stop the timer's action if it's still waiting
#     """
#
#     def __init__(self, interval, function, args=[], kwargs={}):
#         Thread.__init__(self)
#         self.interval = interval
#         self.function = function
#         self.args = args
#         self.kwargs = kwargs
#         self.finished = Event()
#         self.resetted = True
#
#     def cancel(self):
#         """Stop the timer if it hasn't finished yet"""
#         self.finished.set()
#
#     def run(self):
#         print("Time: %s - timer running..." % time.asctime())
#
#         while self.resetted:
#             print("Time: %s - timer waiting for timeout in %.2f..." % (time.asctime(), self.interval))
#             self.resetted = False
#             self.finished.wait(self.interval)
#
#         if not self.finished.isSet():
#             self.function(*self.args, **self.kwargs)
#         self.finished.set()
#         print("Time: %s - timer finished!" % time.asctime())
#
#     def reset(self, interval=None):
#         """ Reset the timer """
#
#         if interval:
#             print("Time: %s - timer resetting to %.2f..." % (time.asctime(), interval))
#             self.interval = interval
#         else:
#             print("Time: %s - timer resetting..." % time.asctime())
#
#         self.resetted = True
#         self.finished.set()
#         self.finished.clear()
#
#
#
# #
# # Usage examples
# #
# def hello():
#     print("Time: %s - hello, world" % time.asctime())
#
# if __name__ == "__main__":
#     # # No reset
#     # print("Time: %s - start..." % time.asctime())
#     # tim = TimerReset(5, hello)
#     # tim.start()
#     # print("Time: %s - sleeping for 10..." % time.asctime())
#     # time.sleep (10)
#     # print("Time: %s - end..." % time.asctime())
#     #
#     # print("\n\n")
#
#     # With Reset
#     print("Time: %s - start..." % time.asctime())
#     tim = TimerReset(5, hello)
#     tim.start()
#     print("Time: %s - sleeping for 4..." % time.asctime())
#     time.sleep (4)
#     tim.reset()
#     print("Time: %s - sleeping for 10..." % time.asctime())
#     time.sleep (10)
#     print("Time: %s - end..." % time.asctime())
#
#     print("\n\n")
#     #
#     # # With reset interval
#     # print("Time: %s - start..." % time.asctime())
#     # tim = TimerReset(5, hello)
#     # tim.start()
#     # print("Time: %s - sleeping for 4..." % time.asctime())
#     # time.sleep (4)
#     # tim.reset (9)
#     # print("Time: %s - sleeping for 10..." % time.asctime())
#     # time.sleep (10)
#     # print("Time: %s - end..." % time.asctime())
import time
from threading import Timer, Thread, Event


# def func():
#     global count
#     print(str(count) + "进入")
#     count += 1
#     if count==3:
#         t.reset(1)
#         count=0
#     # if count==5:
#     #     t.cancel()


class RepeatingTimer(Timer):
    def __init__(self, interval, function, args=[], kwargs={}):
        Thread.__init__(self)
        self.interval = interval
        self.function = function
        self.args = args
        self.kwargs = kwargs
        self.finished = Event()
        self.resetted = True
    def run(self):
        print("Time: %s - timer running..." % time.asctime())

        while self.resetted:
            print("Time: %s - timer waiting for timeout in %.2f..." % (time.asctime(), self.interval))
            self.resetted = False
            self.finished.wait(self.interval)

        while not self.finished.is_set():
            self.function(*self.args, **self.kwargs)
            self.finished.wait(self.interval)
            # print("Time: %s - timer finished!" % time.asctime())

    def reset(self, interval=None):
        """ Reset the timer """

        if interval:
            print("Time: %s - timer resetting to %.2f..." % (time.asctime(), interval))
            self.interval = interval
        else:
            print("Time: %s - timer resetting..." % time.asctime())

        self.resetted = True
        self.finished.set()
        self.finished.clear()

if __name__ == "__main__":
    t = RepeatingTimer(5, func)
    t.start()